package com.homesoft.runtime.core.loaders;

import java.io.InputStream;
import java.util.Dictionary;
import java.util.Enumeration;
import java.util.Hashtable;

public class MetadataRuntimeInfoDictionary {
	private Dictionary<RuntimeClassInfoKey, MetadataRuntimeInfo> metadataRuntimeMappings;
	
	public MetadataRuntimeInfoDictionary()
    {
        metadataRuntimeMappings = new Hashtable<RuntimeClassInfoKey, MetadataRuntimeInfo>(10000);
    }
	
	public Enumeration<MetadataRuntimeInfo> getAllRuntimeMetadataInfos() {
		return metadataRuntimeMappings.elements();
	}
	
	public MetadataRuntimeInfo getRuntimeInfo(String metadataName) {
        return getValue(metadataName, false);
    }
	
	private MetadataRuntimeInfo getValue(String metadataName, boolean createIfNotExisted)
    {
		MetadataRuntimeInfo info = null;
		
        if (metadataName == null) {
        	// TODO 需要实现Runtime异常
            //throw new GTPRuntimeException(RuntimeErrorCode.INVALID_METADATA_NAME, "(null)");
        }

        RuntimeClassInfoKey key = new RuntimeClassInfoKey(metadataName);
        
        info = metadataRuntimeMappings.get(key);
        
        if (info != null && createIfNotExisted) {
        	info = new MetadataRuntimeInfo(metadataName);
        	metadataRuntimeMappings.put(key, info);
        }
        
        return info;
    }
	
	/**
	 * 添加一个运行期类型
	 * @param metadataName
	 * @param folder
	 * @param type
	 * @param metadataRuntimeType
	 * @param dependency
	 */
    public void add(String metadataName, LoadFolder folder, Object obj, MetadataRuntimeType metadataRuntimeType, Dependency dependency) {
        MetadataRuntimeInfo info = getValue(metadataName, true);
        info.addRuntimeType(metadataRuntimeType, folder, obj, dependency);
    }
    
    /**
     * 添加一个资源
     * @param metadataName
     * @param folder
     * @param resourceLoader
     * @param dependency
     */
    public void add(String metadataName, LoadFolder folder, MetadataLoader resourceLoader, Dependency dependency) {
        MetadataRuntimeInfo info = getValue(metadataName, true);
        info.setMetadataLoader(folder, resourceLoader, dependency);
    }
    
    public Object getRuntimeClass(String metadataName, MetadataRuntimeType metadataRuntimeType) {
        MetadataRuntimeInfo info = getValue(metadataName, false);
        
        if (info == null) {
        	return null;
        }
        
        return info.getRuntimeType(metadataRuntimeType);
    }

    public InputStream getStream(String metadataName) {
        MetadataRuntimeInfo info = getValue(metadataName, false);
        
        if (info == null) {
        	return null;
        }
        
        return info.getStream();
    }

    public InputStream getStream(String metadataName, LoadFolder folder) {
        MetadataRuntimeInfo info = getValue(metadataName, false);
        
        if (info == null) {
        	return null;
        }
        
        return info.getStream(folder);
    }

    public void clear() {
        metadataRuntimeMappings = new Hashtable<RuntimeClassInfoKey, MetadataRuntimeInfo>(10000);
    }
}
